const express = require('express');

const router = express.Router();

const {
  getInterface,
  postInterface,
  putInterface,
  deleteInterface,
} = require('../controllers/interviewQController');

const table = 'interview_questions';

const createMiddleware = (fields, table, fn) => {
  fn.param1 = fields;
  fn.param2 = table;
  return fn;
};

const getMiddleware = createMiddleware(['*'], table, function fn(req, res, next) {
  req.fields = fn.param1;
  req.table = fn.param2;
  next();
});

const postMiddleware = createMiddleware(
  ['question', 'category_id'],
  table,
  function fn(req, res, next) {
    req.fields = fn.param1;
    req.table = fn.param2;
    next();
  },
);

const putMiddleware = createMiddleware(
  ['id', 'question', 'category_id'],
  table,
  function fn(req, res, next) {
    req.fields = fn.param1;
    req.table = fn.param2;
    next();
  },
);

const deleteMiddleware = createMiddleware(['id'], table, function fn(req, res, next) {
  req.fields = fn.param1;
  req.table = fn.param2;
  next();
});

router.get('/', getMiddleware, getInterface);
router.post('/', postMiddleware, postInterface);
router.put('/:id', putMiddleware, putInterface);
router.delete('/:id', deleteMiddleware, deleteInterface);

module.exports = router;
